﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class UserManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //hdnUserID.Value = Session["USER_USERID"].ToString();
                BindData();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }

    private void BindData()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_User_SELECTBYSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Shop_ID", SqlDbType.VarChar).Value = Session["SHOP_ID"].ToString();

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            grdUserManagement.AllowPaging = true;
            grdUserManagement.DataSource = dt;
            grdUserManagement.DataBind();

            grdUserManagement.Visible = true;
        }
        else
        {
            grdUserManagement.Visible = false;
        }
    }

    private int idxRow;
    protected void grdUserManagement_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //Binding.GridViewRowDataBound()
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                if (lblStatus.Text == "True")
                {
                    lblStatus.Text = "Active";
                }
                else
                {
                    lblStatus.Text = "Inactive";
                }

                Label lblNo = (Label)e.Row.FindControl("lblNo");
                lblNo.Text = idxRow.ToString();

                Label lblUserID = (Label)e.Row.FindControl("lblUserID");

                ImageButton btnPromotionView = (ImageButton)e.Row.FindControl("btnPromotionView");
                btnPromotionView.PostBackUrl = "UserManagementDetail.aspx?ID=" + lblUserID.Text + "&action=view";

                ImageButton btnPromotionEdit = (ImageButton)e.Row.FindControl("btnPromotionEdit");
                btnPromotionEdit.PostBackUrl = "UserManagementDetail.aspx?ID=" + lblUserID.Text + "&action=edit";

                ImageButton btnPromotionDelete = (ImageButton)e.Row.FindControl("btnPromotionDelete");
                btnPromotionDelete.Attributes.Add("onclick", "return confirm('ยืนยันการลบข้อมูล?');");
            }
            idxRow++;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void grdUserManagement_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Del")
            {
                string strKey = e.CommandArgument.ToString();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();

                SqlCommand oCommand = new SqlCommand("sp_Deal_User_DELETE", oDBConn.DBConnection);
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.Parameters.Add("@User_ID", SqlDbType.BigInt).Value = strKey;
                Int32 intPromotion = oCommand.ExecuteNonQuery();

                oDBConn.Disconnect();

                lblMessage.Text = "<br>ลบข้อมูลเรียบร้อยแล้ว<br>";
                BindData();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserManagementDetail.aspx?action=add");
    }

}
